package com.wpx.log.core.before;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.wpx.log.core.LogContext;
import com.wpx.log.core.LogRequestContext;
import com.wpx.log.core.annotation.OpLog;
import com.wpx.log.util.Utils;

/**
 * 处理 去旧值
 */
@Component
public class TakeOldObjectBefore implements IBefore {
	
	@Autowired
	private EvaluationContext beanFactoryEvaluationContext;
	
	@Override
	public void execute(LogContext logContext, LogRequestContext requestContext, ProceedingJoinPoint point) {
		OpLog opLogAnnotation = requestContext.getOpLogAnnotationAsMethod();
		String oldObjEl = opLogAnnotation.oldObj();
		if(StringUtils.isEmpty(oldObjEl)) {
			return;
		}
		Object takeObject = takeObject(requestContext, oldObjEl);
		
		requestContext.setOld(takeObject);
	}
	/**
	 * 补充参数，并执行bean的方法 取到 对象
	 */
	private Object takeObject(LogRequestContext requestContext, String expressionString) {	
		String value = requestContext.parseArgsExpression(expressionString);						
		return Utils.getValue(beanFactoryEvaluationContext, value, true);
	}
}
